﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
using KhuyenMaiVang.DAL;
using KhuyenMaiVang.Areas.Admin.Models;

namespace KhuyenMaiVang.Helpers
{
    public static class ProcessHelper
    {
        public static string md5AddSecret(string strChange)
        {
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            // Conver the original password to bytes; then create the hash
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(strChange);
            encodedBytes = md5.ComputeHash(originalBytes);

            // Bytes to string
            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
        }
        public static bool checkUsernameOrEmail(int id, string user, string email)
        {
            KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();
            List<User> lstUser = db.Users.ToList();
            if (lstUser.Exists(m=>(m.Email==email || m.Username == user) && m.ID != id))
            {
                return true;
            }
            return false;
        }
        public static bool checkExistVoucher(int id, string alias)
        {
            KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();
            List<Voucher> lstVouchers = db.Vouchers.ToList();
            if (lstVouchers.Exists(m => (m.URLAlias == alias) && m.ID != id))
            {
                return true;
            }
            return false;
        }
        public static ResUser userToResUser(User inUser)
        {
            ResUser result = new ResUser();
            result.ID = inUser.ID;
            result.Username = inUser.Username;
            result.Email = inUser.Email;
            result.DisplayName = inUser.DisplayName;
            result.Avatar = inUser.Avatar;
            result.Phone = inUser.Phone;
            result.Status = inUser.Status;
            result.GroupID = inUser.GroupID;
            return result;
        }
        public static User resUserToUser(ResUser inUser)
        {
            User result = new User();
            result.ID = inUser.ID;
            result.Username = inUser.Username;
            result.Email = inUser.Email;
            result.DisplayName = inUser.DisplayName;
            result.Avatar = inUser.Avatar;
            result.Phone = inUser.Phone;
            result.Status = inUser.Status;
            result.GroupID = inUser.GroupID;
            return result;
        }
        public static User getUser(String email)
        {
            KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();
            return db.Users.Single(m=>m.Email == email);
        }
    }
}